package com.edward.weather.strategy.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.edward.weather.model.WeatherData;
import com.edward.weather.strategy.WeatherApiStrategy;
import com.edward.weather.vo.request.WeatherRequestVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component("openweatherMap")
public class OpenWeatherMapStrategy implements WeatherApiStrategy {


    @Value("${openweathermap.api.url}")
    private String apiUrl;

    @Override
    public WeatherData getWeather(String city) {
        String url = String.format(apiUrl, city);
        HttpResponse httpResponse = HttpRequest.get(url).execute();
        String responseStr = httpResponse.body();
        return parseWeatherData(responseStr);
    }

    @Override
    public WeatherData dayWeather(WeatherRequestVO weatherRequestVO) {
        return null;
    }

    @Override
    public WeatherData parseWeatherData(String responseStr) {
//        JSONObject jsonObject = JSONUtil.parseObj(responseStr);
//        WeatherData weatherData = new WeatherData();
//
//        JSONObject mainMap = jsonObject.getJSONObject("main");
//        //湿度
//        weatherData.setHumidity(mainMap.getInt("humidity"));
//        //压力
//        weatherData.setPressure(mainMap.getInt("pressure"));
//        //温度
//        weatherData.setTemperature(mainMap.getDouble("temp"));
//        //最低温
//        weatherData.setTempMin(mainMap.getDouble("temp_min"));
//        //最高温
//        weatherData.setTempMax(mainMap.getDouble("temp_max"));
//        //城市名字
//        weatherData.setCity(jsonObject.getStr("name"));
//
//        weatherData.setWindSpeed(jsonObject.getJSONObject("wind").getDouble("speed"));
//
//        weatherData.setVisibility(jsonObject.getInt("visibility"));
//        JSONArray jsonArray = jsonObject.getJSONArray("weather");
//        JSONObject weatherMap = jsonArray.getJSONObject(0);
//        weatherData.setDescription(weatherMap.getStr("description"));

        return null;
    }
}